package com.hushao.aimi.service.impl;
import com.hushao.aimi.bo.ProductBo;
import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.entity.AimiCategory;
import com.hushao.aimi.enumVo.ProductRecommendTypeEnum;
import com.hushao.aimi.enumVo.StatusEnum;
import com.hushao.aimi.vo.CategoryVo;
import com.hushao.aimi.vo.ProductVo;
import com.hushao.aimi.repository.CategoryRepository;
import com.hushao.aimi.repository.ProductRespository;
import com.hushao.aimi.repository.RecommendRepository;
import com.hushao.aimi.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 分类信息
 * @author: 胡少
 * @create: 2018-06-08 13:10
 **/
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private ProductRespository productRespository;

    private RecommendRepository recommendRepository;
    @Override
    public ServerResponse findCategory(int categoryId) {
        boolean status=true;
        List<AimiCategory> categories=categoryRepository.findByParentIdAndStatus(categoryId,status);
        List<CategoryVo> categoryVos=categories.stream().map(c->new CategoryVo(c.getId(),c.getName())).collect(Collectors.toList());
        if(categoryId!=0){
            //获取分类的商品
            for (CategoryVo cv:categoryVos) {
                List<ProductVo> productVos=productRespository.listCategoryAndProduct(cv.getId(),
                        StatusEnum.PRODUCT_ON_SALE.getStatus(),
                        StatusEnum.RECOMMEND_NORMAL.getStatus(),
                        ProductRecommendTypeEnum.CATEGORY_RECOMMEND.getCode())
                        .stream().map(ProductBo::new)
                        .map(p->new ProductVo(p.getProductId(),p.getProductName(),p.getRecommendImage()))
                        .collect(Collectors.toList());
                cv.setProductVos(productVos);
            }
            return ServerResponse.Success(categoryVos);
        }
        return ServerResponse.Success(categoryVos);

    }
}

